home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / hypercrd / xcmds / dvlprstc.hqx / Developer Stack 1.3r / card_46070.txt < prev    next >
Encoding:
Text File  |  1991-04-30  |  1.4 KB  |  54 lines

  1. -- card: 46070 from stack: in.3r
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 2202
  5. -- name: CheckString
  6.  
  7.  
  8. -- part contents for background part 10
  9. ----- text -----
  10. 16
  11.  
  12. -- part contents for background part 19
  13. ----- text -----
  14. Functions
  15.  
  16. -- part contents for background part 3
  17. ----- text -----
  18. CheckString
  19.  
  20. -- part contents for background part 2
  21. ----- text -----
  22. --This function checks a string to make sure it is a valid number.
  23. --Useful for validating entry fields that are supposed to contain numbers only.
  24. --From:
  25. --Peter Sylvan
  26. --65 Valley Road 
  27. --Milton, MA 02186
  28. --
  29. --I would like to thank Peter for sending in this contribution to the Developer Stack. [Steve].
  30.  
  31.  
  32. function CheckString entry
  33.   -- function to check if argument is a valid number
  34.   -- returns the number of digits if a number, returns empty if not
  35.   put 0 into Ndp  -- number of decimal points in entry (0 or 1 is OK)
  36.   repeat with n = 1 to length of entry
  37.     put char n of entry into ch
  38.     if ch is "." then
  39.       add 1 to Ndp
  40.       if Ndp is 2 then
  41.         return empty
  42.         exit CheckString
  43.       end if
  44.       next repeat
  45.     end if
  46.     -- the offset function is used here to check each character in the
  47.     -- input against a list of valid characters ("1234567890")
  48.     if offset(ch,"1234567890") is "0" then   -- ch is not a digit
  49.       return empty
  50.       exit CheckString
  51.     end if
  52.   end repeat
  53.   return length of entry - Ndp
  54. end CheckString